package edu.cuit.svsp.controller;

import edu.cuit.svsp.entity.Category;
import edu.cuit.svsp.request.ResponseResult;
import edu.cuit.svsp.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @className: CategoryController
 * @author: pjy
 * @description: 分类控制
 * @date: 2024/7/3
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/all")
    public ResponseResult all() {
        List<Category> categories = categoryService.findAll();
        return new ResponseResult(HttpStatus.OK.value(), "查询分类成功", categories);
    }

    // 查询分类
    @GetMapping("/list")
    public ResponseResult list() {
        List<Category> categories = categoryService.list();
        return new ResponseResult(HttpStatus.OK.value(), "success", categories);
    }

    // 禁用分类
    @PostMapping("/lock/{id}")
    public ResponseResult lock(@PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",categoryService.lock(id));
    }

    // 启用分类
    @PostMapping("/pass/{id}")
    public ResponseResult pass(@PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",categoryService.pass(id));
    }

    // 编辑分类
    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody Category category) {
//        log.info("category:{}", category);
        if(categoryService.edit(category)==false){
            return new ResponseResult(HttpStatus.FORBIDDEN.value(), "分类已存在", false);
        }
        return new ResponseResult(HttpStatus.OK.value(), "success",true);
    }

    // 查询分类
    @GetMapping("/search")
    public ResponseResult search(String name) {
        return new ResponseResult(HttpStatus.OK.value(), "success",categoryService.search(name));
    }
}
